const http = require('../../network/http')
const api = require('../../network/api')
// pages/rechange.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    merchantId: '',
    storeId: '',
    numberFocus: false,
    numberValue: '',
    showMoney: false,
    memberId: '',
    couponId: '',
    chooseText: '请选择优惠券',
    userData: {},
    chooseNum: 1,
    showNum: false, 
  },
  numInput: function (e) {
    this.setData({
      numberValue: e.detail.value
    })
    console.log(this.data.numberValue + "--------------")
  },
  closeview: function(e){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 用户资料查询
   */
  checkUser: function(e){
    let that = this;
    if(this.data.numberValue == ''){
      wx.showToast({
        title: '请先输入用户手机号',
        icon: "none",
        duration: 2000
      });
    }else{
      http.request({
        showLoading: true,
        url: api.userSearch,
        method: "GET",
        data: {
          merchantId: this.data.merchantId,
          storeId: this.data.storeId,
          mobile: this.data.numberValue,
        },
      }).then(res => {
        if (res.data.code == 200) {
          that.setData({
            showMoney: true,
            'userData': res.data.result,
            memberId: res.data.result.memberId,
          })
        }else if(res.data.code == 1001){
          wx.showToast({
            title: '当前用户未注册',
            icon: "none",
            duration: 2000
          });
          that.setData({
            numberValue: "",
          })
        }else{
          wx.showToast({
            title: '请求失败,请稍后再试',
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        console.log(err)
      });
    }
  },
  /**
   * 确认赠送
   */
  doSend: function(e){
    if(this.data.couponId == ''){
      wx.showToast({
        title: '请先选择优惠券',
        icon: "none",
        duration: 2000
      });
    }else{
      var couponIdList = [];
      for(var i = 0 ; i < this.data.chooseNum; i++){
        couponIdList.push(this.data.couponId);
      }
      http.request({
        showLoading: true,
        shake: true,
        url: api.sendCoupon,
        method: 'POST',
        data: {
          memberId: this.data.memberId,
          couponIdList: couponIdList,
        },
      }).then(res => {
        if (res.data.code == 200) {
          setTimeout(function () {
            wx.showToast({
              title: '赠送成功',
              icon: "none",
              duration: 2000
            });
          }, 1000);
          wx.navigateBack({
            delta: 1,
          })
        }else{
          wx.showToast({
            title: '请求失败,请稍后再试',
            icon: "none",
            duration: 2000
          });
        }
      }).catch(err => {
        console.log(err)
      });
    }
  },
  reset: function(e){
    this.setData({
      showMoney: false,
      numberFocus: true
    })
  },
  chooseCoupon: function(e){
    var storeId = this.data.storeId;
    wx.navigateTo({
      url: '/pages/chooseCoupon/chooseCoupon?storeId=' + storeId,
    })
  },
  minusNum: function(e){
    if(this.data.chooseNum == 1){
      wx.showToast({
        title: '赠送数量不能小于1',
        icon: "none",
        duration: 2000
      });
    }else{
      var newNum = this.data.chooseNum - 1;
      this.setData({
        chooseNum: newNum,
      })
    }
    
  },
  addNum: function(e){
    var newNum = this.data.chooseNum + 1;
    this.setData({
      chooseNum: newNum,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    var that = this;
    console.log(options);
    if(options.mobile == null){
      that.setData({
        merchantId: options.merchantId,
        storeId: options.storeId,
        numberFocus: true,
      })
    }else{
      that.setData({
        merchantId: options.merchantId,
        storeId: options.storeId,
        numberValue: options.mobile,
      })
      that.checkUser();
    }
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})